package lormi.meipin.service.impl;


import com.alibaba.fastjson.JSONObject;
import io.rong.models.SdkHttpResult;
import lormi.meipin.common.LMRongYunAPI;
import lormi.meipin.entity.MerChant;
import lormi.meipin.entity.Talent;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.IRongyunService;
import lormi.meipin.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 融云
 * @author ThreeFeng
 * @date 2016年8月12日 下午5:46:13
 */
@Service
public class RongyunService extends BaseService implements IRongyunService{

	@Override
	public Map<String, String> getToken(Integer userid, Integer usertype) throws LMException {
		Map<String,String> map = new HashMap<>();
		String username;
		String photo;
		if (usertype==1) {
			Talent talent = talentMapper.selectByMemId(userid);
			username = talent.getName();
			photo = talent.getPhoto();
		}else {
			MerChant merChant = merChantMapper.selectByMerId(userid);
			username = merChant.getName();
			photo = merChant.getPhoto();
		}
		
		SdkHttpResult rongyun = LMRongYunAPI.getRongYunToken(String.valueOf(userid), username, photo);
		JSONObject rongyunjson = JsonUtil.getJsonObject(rongyun.getResult());
		if(rongyun.getHttpCode()==200){
			map.put("rongyuncode", rongyunjson.getString("code"));
			map.put("userid", rongyunjson.getString("userId"));
			map.put("token", rongyunjson.getString("token"));
		}else{
			map.put("rongyuncode", rongyun.getHttpCode()+"");
		}
		return map;
	}

}
